//
//  AinvitiveDisViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/14.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "AinvitiveDisViewController.h"
#import "AinvitiveListTableViewCell.h"
#import "AinvitivePersonModel.h"
#import "SeleAddressView.h"
#import "AprofitContentViewController.h"
#import "PGDatePickManager.h"
#import "HmSelectAdView.h"
#import "ABshopDetaileViewController.h"
#import "AInviteManCell.h"

@interface AinvitiveDisViewController ()<UITableViewDelegate,UITableViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *mydateSource;
@property(nonatomic,strong)UIButton *seleaddressBut;
@property(nonatomic,strong)UIButton *seletimeBut;
@property(nonatomic,strong)SeleAddressView *seleAddressView;
@property(nonatomic,strong)UILabel *moneyLB;//
@property(nonatomic,strong)UIButton *lefttimeLB;
@property(nonatomic,strong)UIButton *righttimeLB;
@property(nonatomic,strong)UIView *bgview;
@property(nonatomic,assign)BOOL isleft;
@property(nonatomic,strong)UILabel *leftLB;
@property(nonatomic,strong)UILabel *rightLB;
@property (nonatomic, strong) NSString *currentProvince;
@property (nonatomic, strong) NSString *currentCity;
@property (nonatomic, strong) NSString *currentArea;

@end

@implementation AinvitiveDisViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isleft = YES;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    self.title = @"管理商";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    [self.myTableView reloadData];
    
    [self request];
}

-(void)showheader{
    UIView *head = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 45)];
    head.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    
    UILabel *contertLb = [[UILabel alloc]init];
    contertLb.textAlignment = NSTextAlignmentCenter;
    contertLb.textColor = zhutiColor;
    contertLb.font = FontSize(16);
    [head addSubview:contertLb];
    [contertLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(12);
        make.right.offset(-16);
        make.height.offset(24);
    }];
    self.moneyLB=contertLb;
    self.seleaddressBut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.seleaddressBut.layer.masksToBounds = YES;
    self.seleaddressBut.layer.cornerRadius = 13;
    self.seleaddressBut.titleLabel.font = FontSize(16);
    [self.seleaddressBut setBackgroundColor:[UIColor whiteColor]];
    [self.seleaddressBut setTitle:@"全部地区" forState:(UIControlStateNormal)];
    CGFloat www =    [self widthForString:@"全部地区" fontSize:13 andHeight:18 ] +29;
    self.seleaddressBut.frame = CGRectMake(10, 10, www, 26);
    [self.seleaddressBut setImage:imgname(@"xiangxiaImg") forState:(UIControlStateNormal)];
    [ self.seleaddressBut setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.seleaddressBut.imageView.frame.size.width, 0,  self.seleaddressBut.imageView.frame.size.width)];
    [self.seleaddressBut setImageEdgeInsets:UIEdgeInsetsMake(11.5, self.seleaddressBut.titleLabel.bounds.size.width+5, 11.5, - self.seleaddressBut.titleLabel.bounds.size.width)];
    [self.seleaddressBut setTitleColor:[UIColor colorWithHexString:@"#4A4A4A"] forState:(UIControlStateNormal)];
    [head addSubview:self.seleaddressBut];
    [self.seleaddressBut addTarget:self action:@selector(seleAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.myTableView.tableHeaderView = head;
}

-(void)seleAction:(UIButton *)but{
    
    HmSelectAdView *selectV = [[HmSelectAdView alloc] initWithLastContent:self.currentProvince ? @[self.currentProvince, self.currentCity, self.currentArea] : nil];
    selectV.confirmSelect = ^(NSArray *address) {
        self.currentProvince = address[0];
        self.currentCity = address[1];
        self.currentArea = address[2];
        
        NSString *titleStr = [NSString stringWithFormat:@"%@ %@",self.currentCity,self.currentArea];
        CGFloat www =    [self widthForString:titleStr fontSize:13 andHeight:18 ] +35;
        self.seleaddressBut.frame = CGRectMake(10, 10, www, 26);
        [self.seleaddressBut setTitle:titleStr forState:(UIControlStateNormal)];
        [self.seleaddressBut setImage:imgname(@"xiangxiaImg") forState:(UIControlStateNormal)];
        [ self.seleaddressBut setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.seleaddressBut.imageView.frame.size.width, 0,  self.seleaddressBut.imageView.frame.size.width)];
        [self.seleaddressBut setImageEdgeInsets:UIEdgeInsetsMake(11.5, self.seleaddressBut.titleLabel.bounds.size.width+5, 11.5, - self.seleaddressBut.titleLabel.bounds.size.width)];
        [self.seleaddressBut setTitleColor:zhutiColor forState:(UIControlStateNormal)];
        [self request];
    };
    [selectV show];
}

-(float)widthForString:(NSString *)value fontSize:(float)fontSize andHeight:(float)height{CGSize sizeToFit = [value sizeWithFont:[UIFont systemFontOfSize:fontSize] constrainedToSize:CGSizeMake(CGFLOAT_MAX, height) lineBreakMode:NSLineBreakByWordWrapping];//此处的换行类型（lineBreakMode）可根据自己的实际情况进行设置
    return sizeToFit.width;
}

-(void)request{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    
    param[@"area"] = NOTNIL(self.currentProvince);

    [NetWorkTool POST:getAgentLevelB param:param success:^(id dic) {
        [self.mydateSource removeAllObjects];
        self.moneyLB.text = [NSString stringWithFormat:@"合计:%ld家", [[[dic objectForKey:@"data"] objectForKey:@"count"] integerValue]];
        for (NSMutableDictionary *mudic in [[dic objectForKey:@"data"] objectForKey:@"list"]) {
            [DYModelMaker DY_makeModelWithDictionary:mudic modelKeyword:@"" modelName:@""];
            AinvitivePersonModel *model = [[AinvitivePersonModel alloc]init];
            [model setValuesForKeysWithDictionary:mudic];
            [self.mydateSource addObject:model];
        }
        [self.myTableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        self.moneyLB.text = @"合计:0家";
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        
    } fail:^(NSError *error) {
        self.moneyLB.text = @"合计:0家";
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        
    } needUser:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.mydateSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 86;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AInviteManCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AInviteManCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    AinvitivePersonModel *model = _mydateSource[indexPath.section];
    [cell refash:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //暂时禁止跳转
    //    AinvitivePersonModel *model = _mydateSource[indexPath.section];
    //    ABshopDetaileViewController *VC= [[ABshopDetaileViewController alloc]init];
    //    VC.shopid = model.Id;
    //    [self.navigationController pushViewController:VC animated:YES];
}

-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight) style:(UITableViewStylePlain)];
        _myTableView.backgroundColor = tableViewDefaultColor;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        //_myTableView.estimatedRowHeight = 90;
        //_myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.tableFooterView = [UIView new];
        //_myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
            [self->_myTableView.mj_header beginRefreshing];
        }];
        [_myTableView registerNib:[UINib nibWithNibName:@"AInviteManCell" bundle:nil] forCellReuseIdentifier:@"AInviteManCell"];
        adjustInset(_myTableView);
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

-(void)leftActionsss{
    self.leftLB.backgroundColor = zhutiColor;
    [self.lefttimeLB setTitleColor:zhutiColor forState:(UIControlStateNormal)];
    self.rightLB.backgroundColor = [UIColor colorWithHexString:@"#999999"];
    [self.righttimeLB setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:(UIControlStateNormal)];
    self.isleft = YES;
}

-(void)righttimeLBActionsss{
    self.isleft = NO;
    self.rightLB.backgroundColor = zhutiColor;
    [self.righttimeLB setTitleColor:zhutiColor forState:(UIControlStateNormal)];
    self.leftLB.backgroundColor = [UIColor colorWithHexString:@"#999999"];
    [self.lefttimeLB setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:(UIControlStateNormal)];
}

@end
